<?php

class UserController extends Zend_Controller_Action 
{
    function init()
    {
        $this->initView();
        Zend_Loader::loadClass('AuxDbTable');
        Zend_Loader::loadClass('User');
        Zend_Loader::loadClass('Authgroup');
        Zend_Loader::loadClass('Mainmenu');
        Zend_Loader::loadClass('Rights');
        Zend_Loader::loadClass('Authority'); 
        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->user = Zend_Auth::getInstance()->getIdentity();
        $this->view->preferences = array('any'=>'any','individ'=>'individ', 'group'=>'group');
        $this->view->authgroup_ids = array('1'=>'Student','2'=>'Teacher', '3'=>'Admin');
    }
    
    function preDispatch()
    {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('auth/login');
        }
        $menu = new Mainmenu();
        $this->view->main_menu = $menu->getMenu($this->view->user->id);
    }

    function indexAction()
    {
        $Rights = new Rights();
        if(!$Rights->CanDo($this->view->user->id,'user','index')) $this->_redirect(DEFAUL_URL);
        
        $this->view->title = "Users";
        $this->view->content_title = "Users";
        $user = new User();
        
        $this->view->item = new stdClass();
        
        $this->view->item->data = $this->_request->getParam('data', 0)?$this->_request->getParam('data', 0):'';
        $this->view->item->authgroup_id = $this->_request->getParam('authgroup_id', 0)?$this->_request->getParam('authgroup_id', 0):0;
        
        $cond = array('deleted!=1');
        foreach($this->view->item as $key=>$value) {
            if($value) {
                switch($key) {
                    case 'data':
                        $cond[] = ' first_name like "%'.$value.'%" OR 
                                    last_name like "%'.$value.'%" OR 
                                    contacts like "%'.$value.'%" OR 
                                    email like "%'.$value.'%" ';
                        break;
                    default :
                        $cond[] = $key.'="'.$value.'"';
                        break;
                }
            }
        }
        
        $this->view->users = $user->fetchAll($cond,array('authgroup_id','last_name'))->toArray();
        foreach($this->view->users as &$u){
            $u['hasDelay'] = $u['authgroup_id']==1?$user->hasDelayPayment($u['id']):false;
            
            $u['authgroup_id'] = $this->view->authgroup_ids[$u['authgroup_id']];
            
        }
        
        
        $this->view->can_add_user = $Rights->CanDo($this->view->user->id,'user','add');
        $this->view->can_edit_user = $Rights->CanDo($this->view->user->id,'user','edit');
        $this->view->can_delete_user = $Rights->CanDo($this->view->user->id,'user','delete');
        
        $this->render();
    }
    
    
    function addAction()
    {
        $obj = new User(); 
        $Rights = new Rights();
        if(!$Rights->CanDo($this->view->user->id,'user','add')) $this->_redirect(DEFAUL_URL);
        
        $this->view->content_title = $this->view->title = "Add New User";
        $params = array();
        
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            Zend_Loader::loadClass('Zend_Filter_StripTags');
            $filter = new Zend_Filter_StripTags();
            
        	$params['authgroup_id'] = trim($filter->filter($this->_request->getPost('authgroup_id')));
            $params['first_name'] = trim($filter->filter($this->_request->getPost('first_name')));
            $params['last_name'] = trim($filter->filter($this->_request->getPost('last_name')));
            $params['address'] = trim($filter->filter($this->_request->getPost('address')));
            $params['contacts'] = trim($filter->filter($this->_request->getPost('contacts')));
            $params['email'] = trim($filter->filter($this->_request->getPost('email')));
            $params['username'] = trim($filter->filter($this->_request->getPost('username')));
            $params['password'] = trim($filter->filter($this->_request->getPost('password')));
            $params['phone'] = trim($filter->filter($this->_request->getPost('phone')));
            $params['register_date'] = date('Y-m-d H:i:s');
            $params['birthday'] = trim($filter->filter($this->_request->getPost('birthday')));
            
            $params['individ_price'] = (float)trim($filter->filter($this->_request->getPost('individ_price')));
            $params['group_price'] = (float)trim($filter->filter($this->_request->getPost('group_price')));
            
            $this->view->message = $obj->validate(0,$params);
        	if($this->view->message===true) {
    			
                
    			$new_id = $obj->insert($params);
                
        	    $this->_redirect('/user');
        	    return;
            }
        } 
        
        // set up an "empty" user
        $this->view->f_user = new stdClass();
        $this->view->f_user->id = 0;
        $this->view->f_user->authgroup_id = isset($params['authgroup_id'])?$params['authgroup_id']:'';
        $this->view->f_user->first_name = isset($params['first_name'])?$params['first_name']:'';
        $this->view->f_user->last_name = isset($params['last_name'])?$params['last_name']:'';
        $this->view->f_user->address = isset($params['address'])?$params['address']:'';
        $this->view->f_user->contacts = isset($params['contacts'])?$params['contacts']:'';
        $this->view->f_user->email = isset($params['email'])?$params['email']:'';
        $this->view->f_user->phone = isset($params['phone'])?$params['phone']:'';
        $this->view->f_user->username = isset($params['username'])?$params['username']:'';
        $this->view->f_user->password = isset($params['password'])?$params['password']:'';
        $this->view->f_user->birthday = isset($params['birthday'])?$params['birthday']:'';
        $this->view->f_user->register_date = date('Y-m-d H:i:s');
        $this->view->f_user->individ_price = isset($params['individ_price'])?$params['individ_price']:0;
        $this->view->f_user->group_price = isset($params['group_price'])?$params['group_price']:0;
        
        // additional view fields required by form
        $this->view->action = 'add';
                
        $this->render();
    }
    
    function editAction()
    {
        $Rights = new Rights();
        if(!$Rights->CanDo($this->view->user->id,'user','edit')) $this->_redirect(DEFAUL_URL);
            
        $this->view->content_title = $this->view->title = "Edit User";
        $obj = new User();
        
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            Zend_Loader::loadClass('Zend_Filter_StripTags');
            $filter = new Zend_Filter_StripTags();
            
    	    $id = (int)$this->_request->getPost('id');
            
    	    $params['authgroup_id'] = trim($filter->filter($this->_request->getPost('authgroup_id')));
            $params['first_name'] = trim($filter->filter($this->_request->getPost('first_name')));
            $params['last_name'] = trim($filter->filter($this->_request->getPost('last_name')));
            $params['address'] = trim($filter->filter($this->_request->getPost('address')));
            $params['contacts'] = trim($filter->filter($this->_request->getPost('contacts')));
            $params['email'] = trim($filter->filter($this->_request->getPost('email')));
            $params['username'] = trim($filter->filter($this->_request->getPost('username')));
            $params['password'] = trim($filter->filter($this->_request->getPost('password')));
            $params['phone'] = trim($filter->filter($this->_request->getPost('phone')));
            $params['birthday'] = trim($filter->filter($this->_request->getPost('birthday')));
            
            $params['individ_price'] = (float)trim($filter->filter($this->_request->getPost('individ_price')));
            $params['group_price'] = (float)trim($filter->filter($this->_request->getPost('group_price')));
            
            if ($id !== false) {
                $this->view->message = $obj->validate($id,$params);
                if($this->view->message===true) {
                    $where = 'id = ' . $id;
				    $obj->update($params, $where);
			    
	    	        $this->_redirect('/user');
	    	        return;
                } else {
                    $this->view->f_user = $obj->fetchRow('id='.$id);
                    
                    $this->view->content_title = "User : ".$this->view->f_user->first_name.' '.$this->view->f_user->last_name;
                    
                    if(isset($params['authgroup_id'])) $this->view->f_user->authgroup_id = $params['authgroup_id'];
                    if(isset($params['first_name'])) $this->view->f_user->first_name = $params['first_name'];
                    if(isset($params['last_name'])) $this->view->f_user->last_name = $params['last_name'];
                    if(isset($params['address'])) $this->view->f_user->address = $params['address'];
                    if(isset($params['contacts'])) $this->view->f_user->contacts = $params['contacts'];
                    if(isset($params['email'])) $this->view->f_user->email = $params['email'];
                    if(isset($params['username'])) $this->view->f_user->username = $params['username'];
                    if(isset($params['password'])) $this->view->f_user->password = $params['password'];
                    if(isset($params['phone'])) $this->view->f_user->phone = $params['phone'];
                    if(isset($params['birthday'])) $this->view->f_user->birthday = $params['birthday'];
                    $this->view->f_user->individ_price = isset($params['individ_price'])?$params['individ_price']:'';
                    $this->view->f_user->group_price = isset($params['group_price'])?$params['group_price']:'';
                    
	            }
            }
        } else {
            $id = (int)$this->_request->getParam('id', 0);
	        if ($id > 0) {
                $this->view->f_user = $obj->fetchRow('id='.$id);
                $this->view->content_title = "User : ".$this->view->f_user->first_name.' '.$this->view->f_user->last_name;
                
	        }
        }

        // additional view fields required by form
        $this->view->action = 'edit';
        $this->view->buttonText = 'Update';
                
        $this->render();
    }
    
    function deleteAction()
    {
        $Rights = new Rights();
        if(!$Rights->CanDo($this->view->user->id,'user','delete')) $this->_redirect(DEFAUL_URL);
        
        $this->view->content_title = $this->view->title = "Delete User";
        
        $obj = new User();
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            Zend_Loader::loadClass('Zend_Filter_Alpha');
            $filter = new Zend_Filter_Alpha();
            
            $id = (int)$this->_request->getPost('id');
            $del = $filter->filter($this->_request->getPost('del'));
            
            if ($del == 'Yes' && $id > 0) {
				$data = array(
                        'deleted' => '1'
                );
                $where = 'id = ' . $id;
                $obj->update($data, $where);
	        }
        } else {
            $id = (int)$this->_request->getParam('id');
		    if ($id > 0) {
		        // only render if we have an id and can find the user.
		        $this->view->f_user = $obj->fetchRow('id='.$id);
		        $this->view->content_title = "User : ".$this->view->f_user->first_name.' '.$this->view->f_user->last_name;
                
		        if ($this->view->f_user->id > 0) {
                    $this->render();
                    return;
		        }
		    }
        }
        
        // redirect back to the project list unless we have rendered the view
   	    $this->_redirect('/user');
    }
    
    function rightsAction()
    {
        $Rights = new Rights();
        if(!$Rights->CanDo($this->view->user->id,'user','rights')) $this->_redirect(DEFAUL_URL);
        
        $user_id = (int)$this->_request->getParam('id');
        
        $rightsObj = new Rights();
        $authObj = new Authority();
        
        $obj = new User();
        $this->view->f_user = $obj->fetchRow('id='.$user_id);
        
        if(!$this->view->f_user) 
        {
            $this->_redirect('/user');
        }
        
        $this->view->title = "User Rights";
        $this->view->content_title = $this->view->f_user->first_name." ".$this->view->f_user->last_name.' : Rights';
        
        
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            
            
            $rightsObj->ClearLinks($user_id);
            
            $authorities = $this->_request->getParam('authorities');
            foreach($authorities as $authority_id)
            {
                $rightsObj->AddRights($user_id,$authority_id);
            }
            $this->_redirect('/user/rights/id/'.$user_id);
        } 
        else 
        {
            $this->view->authorities = $authObj->fetchAll();
            
            $r_links = $rightsObj->fetchAll('user_id='.$user_id);
            $this->view->links = array();
            foreach($r_links as $link)
            {
                $this->view->links[] = $link['authority_id'];
            }
        }
        
        $this->view->action = 'rights';
        $this->render();
    }
    
}